package com.deng.eventflow.core.petitpoucet;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class ProvenanceNode {
    /**
     * The ID of the data point
     */
    protected final NodeFunction m_nodeFunction;

    /**
     * A set of points this data point depends on
     */
    protected final List<ProvenanceNode> m_parents;

    /**
     * A set of points that depend on this data point
     */
    protected final List<ProvenanceNode> m_children;

    /**
     * Creates a new provenance node
     * @param datapoint_id The ID of the data point
     * @param owner The owner of the data point
     */
    public ProvenanceNode(NodeFunction node)
    {
        super();
        m_nodeFunction = node;
        m_parents = new ArrayList<ProvenanceNode>();
        m_children = new ArrayList<ProvenanceNode>();
    }

    /**
     * Adds a parent to this node. Note that this does <strong>not</strong> add the
     * current node as a child in {@code p}.
     * @param p The parent
     */
    public void addParent(ProvenanceNode p)
    {
        m_parents.add(p);
    }

    /**
     * Adds a parent to this node. Note that this does <strong>not</strong> add the
     * current node as a parent in {@code p}.
     * @param p The parent
     */
    public void addChild(ProvenanceNode p)
    {
        m_children.add(p);
    }

    /**
     * Replaces a parent node by another
     * @param position The position where to perform the replacement. The
     * operation is ignored is this value is out of bounds.
     * @param p The parent
     */
    public void replaceParent(int position, ProvenanceNode p)
    {

        m_parents.set(position, p);
    }

    public NodeFunction getNodeFunction()
    {
        return m_nodeFunction;
    }

    public List<ProvenanceNode> getParents()
    {
        return m_parents;
    }

    public List<ProvenanceNode> getChildren()
    {
        return m_children;
    }

    @Override
    public String toString()
    {
        if (m_nodeFunction != null)
        {
            return m_nodeFunction.toString();
        }
        return "?";
    }

    public void addParents(Collection<ProvenanceNode> parents)
    {
        m_parents.addAll(parents);

    }
}
